<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Mail;

class UserContentdata extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */

    public function index()
    {
        $data=\Cookie::get('userdetails');
        $data=\DB::table("userdetails")->where("id",$data['id'])->first();
        $user=\Cookie::get('user');
        $user=\DB::table("user")->where("id",$user['id'])->first();
        $addr=$data['addr'];


        // dd(self::$arr);

       // dd($data);

        return view("home.user.userUpload",["data"=>$data,"user"=>$user]);
    }
    public function ajax()
    {
        $data=\Cookie::get('userdetails');
        $data=\DB::table("userdetails")->where("id",$data['id'])->first();
        $addr=$data['addr'];
        $arr=explode("-",$addr);
        // echo 1111;
        echo json_encode($arr) ;
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data=$request->all();
        $email=$data['email'];
        $user=\Cookie::get('user');
        $id=$user['id'];
        $token=$user['token'];
        self::_mail($email,$id,$token);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id,Request $request)
    {
        // $data=$request->all()['arr'];
        // echo $data['nickname'];
    // echo 111;
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {

         $data=$request->except("_token","_method","photo");
            //获取上传图片名称
        $filename=$request['photo'];

        //对比
        $reg=\DB::table("userdetails")->where('uid',$id)->first();



            if(!empty($filename)){

                     // 文件路径
                     $temp_path = '/Uploads/user';
                     $img = ltrim($temp_path.'/'.$filename);
                     $data['photo']=$img;

                    //删除原有文件
                     $filePath=$reg['photo'];

                    $re = \Storage::disk('public')->delete($filePath);
            }

       $res= \DB::table("userdetails")->where('uid',$id)->update($data);

       if($res){

             return redirect('/userContentdata')->with('success','修改成功');

         }else{

             return back()->with('error','修改失败');
        }

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    /*
     *
     * 激活发送邮件
     *
     */
    static public function _mail($email,$id,$token)
    {
        Mail::send('home.user.emial', ['id' => $id,"email" => $email,'token' => $token], function ($m) use ($email) {
            // $m->from('hello@app.com', 'Your Application');

            $m->to($email)->subject('!联想商城 激活邮件');
        });
    }

    /**
     * 判断是不是本人点击链接,如果是返回true反之false
     * */
    public function jihuo(Request $request)
    {
        // echo 111;
        $data=$request->all();
        $user=\Cookie::get('user');
        // dd($data);
        if($data['id']==$user['id']&&$data['token']==$user['token']){
            \DB::table("user")->where("id",$data['id'])->update(["email"=>$data['email']]);
            echo true;
        }else{
            echo false;
        }
    }

/**
 *
 *执行个人信息修改
 *
 */

  public function alter(Request $request){
    $data=$request->all();
    $user=\Cookie::get('user');
    $res = \DB::table("userdetails")->where("id",$user['id'])->update($data);
    $res=\DB::table("userdetails")->where("id",$user['id'])->update($data);
    // dd($data);
    if($res){
        return 1;
    }else{
        return 0;
    }

    // return $data;

  }


}
